﻿using UnityEngine;
using System.Collections;

public class Peep : MonoBehaviour, ICanSwim {
	
	public Mob mob;
	public Bar bar;
	public int barloc;
	public int barvel;
	public int thirst = 2;

	int drinktimer = 0;
	int distracttimer = 0;
		
	enum PeepState {
		Idle,
		Walking,
		Drinking,
		Retreating,
		Distracted,
	};
	PeepState state = PeepState.Idle;
	
	public void create() {
		mob = new Mob ();
		mob.create ("Peep");
	}
	
	public void init(Bar a) {
		bar = a;
		mob.init (Tappa.game.data.peep);
		mob.pos = a.data.beerEnd.pos;
		mob.upd ();
		barloc = bar.data.barLengthScale;
		barvel = -1;
		state = PeepState.Walking;
		thirst = 2;
	}
	
	public void upd() {		
		if (state == PeepState.Idle) {
			mob.dataSprite = Tappa.game.data.peep;
			barvel = 0;
			if (Random.Range (1,10000) == 1) {
				state = PeepState.Walking;
			}
		} else if (state == PeepState.Walking) {
			barvel = -Tappa.game.data.constants.peepSpeedWalk;
			mob.dataSprite = Tappa.game.data.peepWalking;
			if (Random.Range (1,10000) == 1) state = PeepState.Idle;
			if (barloc < 0) Tappa.game.x_x ();
		}
		if (state == PeepState.Idle || state == PeepState.Walking) {
			float hitbox = 100;
			float hitbox2 = hitbox * hitbox;
			for (int i = 0; i < Tappa.game.beers.size; i++) {
				Beer b = Tappa.game.beers[i];
				if (!b.isEmpty) {
					float dst2 = 0;
					float dx = b.mob.pos.x - mob.pos.x;
					float dy = b.mob.pos.y - mob.pos.y;
					dst2 = dx*dx + dy*dy;
					if (dst2 < hitbox2) {
						Tappa.game.beers.remove(ref i);
						thirst--;
						//if (thirst <= 0) {							
							state = PeepState.Retreating;
							drinktimer = 0;
							barvel = Tappa.game.data.constants.peepSpeedRetreat;
						//} else {
						//	state = PeepState.Drinking;
						//	drinktimer = 0;

						//	Tip t = Tappa.game.tips.get ();
						//	t.init (bar, barloc);

						//}
					}
				}
			}
		}
		if (state == PeepState.Retreating) {
			mob.dataSprite = Tappa.game.data.peepRetreating;
			barvel = Tappa.game.data.constants.peepSpeedRetreat;
			drinktimer+=2;
			if (drinktimer >= Tappa.game.data.constants.peepAnimationTimeRetreat) {
				state = PeepState.Drinking;
				drinktimer = 0;				
				Tip t = Tappa.game.tips.get ();
				t.init (bar, barloc);
			}
		}
		if (state == PeepState.Drinking) {
			mob.dataSprite = Tappa.game.data.peepDrinking;
			barvel = 0;
			drinktimer++;
			if (drinktimer >= Tappa.game.data.constants.peepAnimationTimeDrink) {
				state = PeepState.Idle;
				barvel = 0;
				Beer t = Tappa.game.beers.get ();
				t.init (bar, barloc, true);
			}
		}
		if (state == PeepState.Distracted) {
			Tip.isdance = true;
			barvel = 0;
			mob.dataSprite = Tappa.game.data.peepDistracted;
			distracttimer++;
			if (distracttimer > Tappa.game.data.constants.peepAnimationTimeDistracted) {
				state = PeepState.Idle;
			}
		}

		barloc = barloc + barvel;
		if (barloc < 0) {
			barloc = -barloc;
			barvel = -barvel;
		}
		int len = bar.data.barLengthScale;
		if (barloc > len) {
			if (state == PeepState.Retreating) {
				bar.patrons--;
				isAlive = false;
				Score.add(Score.Value.Peep);
				Score.addPatron();
			}
		}
		
		xy pos;
		xy pos1 = bar.data.beerStart.pos;
		xy pos2 = bar.data.beerEnd.pos;
		float tween = (float)barloc / len;
		pos.x = (int)Mathf.Lerp (pos1.x, pos2.x, tween);
		pos.y = (int)Mathf.Lerp (pos1.y, pos2.y, tween);
		
		mob.pos = pos;
		mob.r.sortingOrder = bar.data.barLengthScale - barloc;
		mob.upd();
		
	}

	public void onStartDance() {
		if (state == PeepState.Walking) {
			state = PeepState.Distracted;
			distracttimer = 0;
		}
	}

	public static void startDance() {
		for (int i = 0; i < Tappa.game.peeps.size; i++) {
			Tappa.game.peeps[i].onStartDance();
		}
	}

	public bool isAlive = false;
	public bool wantsToGoBackInThePool() {
		return !isAlive;
	}
	public void getInThePool() {
		isAlive = false;
		mob.go.SetActive (false);
	}
	public void getOutOfThePool() {
		isAlive = true;
		mob.go.SetActive (true);
	}
	

}
